/*
 * Copyright (c) 2009 by Chances.
 * $HeadURL$:
 * $Author: chenxx $:
 * $Date: 2010/05/25 06:34:06 $:
 * $Rev$:
 */
package chances.iptv.web;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class comments.
 * 
 * @author <a href="mailto:chenxx@chances.com.cn">chenxx</a>
 */
public class PVFilter implements Filter {

    private static final Log log = LogFactory.getLog("indexPVLog");

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain chain) throws IOException, ServletException {
    	chain.doFilter(arg0, arg1);
        HttpServletRequest req = (HttpServletRequest) arg0;
        UserSession userSession = UserSession.getUserSession(req);
        if (null != userSession) {
            String userAgent = req.getHeader("User-agent");
            String logItem[] = new String[] { req.getSession().getId(),
                    userSession.getMainUserId(), userSession.getUserId(),
                    userSession.getEpgGroup(), req.getRemoteAddr(),
                    req.getRequestURL().toString(), req.getHeader("Referer"),
                    userAgent };
            java.text.MessageFormat message = new MessageFormat(
                    "{0},{1},{2},{3},{4},{5},{6},{7}");
            String logs = message.format(logItem);
            log.info(logs);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig arg0) throws ServletException {
    }

}
